<template>

    <view>
		<u-navbar height="60" bgColor="#0081ff">
			<!-- #ifdef MP-WEIXIN -->
			<view class="u-nav-slot margin-top-xl" slot="left">
			</view>
			<!-- #endif -->
			<view slot="center" class="u-nav-slot" >
				<view style=" font-size: 20px; color: #FFFFFF;">共享管理</view>
			</view>
		</u-navbar>
		<u-subsection :list="list" :current="current" bgColor="#0081ff" @change="sectionChange"></u-subsection>

        <view v-if="deviceList != null &&  current == 0" class="cu-list menu card-menu margin-top xl-border"
            v-for="(item,index) in deviceList" :key="index"
            @tap="toShare(item.deviceId,item.deviceName,item.shareStatus)">
            <view class="cu-item arrow">
                <view class="content  flex align-center">
                    <image class="round" style="width: 6vh;height: 6vh;" :src=" imgUrl + item.image"></image>
                    <view class="flex flex-direction margin-left-sm">
                        <text class="text-grey">{{item.deviceName}}</text>
                        <text class="text-grey text-sm" v-if="item.shareNumber > 0">已与{{item.shareNumber}}位用户共享</text>
                        <text class="text-grey text-sm" v-else>未共享</text>
                    </view>
                </view>

            </view>
        </view>

        <view v-if="shareList != null &&   current == 1" class="cu-list menu card-menu margin-top xl-border"
            v-for="(item,index) in shareList" :key="index">
            <view class="cu-item">
                <view class="content flex align-center">
                    <image class="round" style="width: 6vh;height: 6vh;" :src=" imgUrl + item.image"></image>
                    <view class="flex flex-direction margin-left-sm">
                        <text class="text-grey">{{item.username}}</text>
                        <text class="text-grey text-sm" v-if="item.status == 1">{{item.createTime}}</text>
                        <text class="text-grey text-sm" v-if="item.status == 0">等待用户接受</text>
                    </view>
                </view>
                <view class="text-blue" @tap="delAccept(item.shareId)">取消</view>
                <view class="text-blue  margin-left-sm" v-if="item.status == 0" @tap="saveAccept(item.shareId)">接受
                </view>
            </view>
        </view>

        <view v-else class="margin-top flex justify-center text-gray">
            <text v-if="deviceList == null &&  isClick == 'share'">还未绑定设备</text>
            <text v-if="shareList == null &&  isClick == 'accept'">还未有共享的设备</text>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
				imgUrl: this.imgUrl,
                deviceList: null,
                shareList: null,
				list: ['共享', '接受'],
				current: 0,
            }
        },
        onLoad(option) {
            this.getDeviceList()
        },
        methods: {
			sectionChange(index){
				this.current = index;
				if(this.current == 0 ){
					this.share()
				}else{
					this.accept()
				}
			},
            //共享
            share() {
                this.getDeviceList()
                this.isClick = "share"
            },
            //接受
            accept() {
                this.isClick = "accept"
                this.getShareList()
            },
            getShareList() {
                let that = this
                this.$http.request({
                    url:  'app/share/acceptList',
                    method: 'GET',
                    success: (data) => {
                        if (data.data && data.data.code == 0) {
                            that.shareList = data.data.list
                        }
                    }
                })
            },
            //获取用户的设备列表
            getDeviceList() {
                this.$http.request({
                    url:  'app/device/listAll',
                    method: 'GET',
                    success: (data) => {
                        if (data.data && data.data.code == 0) {
                            this.deviceList = data.data.list
                        }
                    }
                })
            },
            //跳转到共享列表
            toShare(deviceId, deviceName, shareStatus) {
                uni.navigateTo({
                    url: '/pages/share/share?deviceId=' + deviceId +
                        "&deviceName=" + deviceName +
                        "&shareStatus=" + shareStatus
                })

            },

            delAccept(shareId) {
                let that = this
                uni.showModal({
                    content: '危险操作，是否继续！',
                    success: function(res) {
                        if (res.confirm) {
                            that.$http.request({
                                url:  'app/share/delete',
                                method: 'GET',
                                data: {
                                    'shareIds': shareId
                                },
                                success(data) {
                                    if (data.data && data.data.code == 0) {
                                        that.getShareList()
                                    }
                                },
                                fail() {
                                    uni.showToast({
                                        title: '服务器维护中....',
                                        position: 'top',
                                        icon: "none"
                                    })
                                }
                            })
                        } else if (res.cancel) {}
                    }
                })
            },

            saveAccept(shareId) {
                let that = this
                this.$http.request({
                    url:  'app/share/setStatus',
                    method: 'GET',
                    data: {
                        'shareId': shareId
                    },
                    success(data) {
                        if (data.data && data.data.code == 0) {
                            that.getShareList()
                        }
                    },
                    fail() {
                        uni.showToast({
                            title: '服务器维护中....',
                            position: 'top',
                            icon: "none"
                        })
                    }
                })
            }
        }
    }
</script>

<style>
</style>
